import "babel-polyfill"
import Vue from 'vue'
import VueRouter from 'vue-router'
import router from './router'
import index from './app.vue'
import '@/assets/css/mobile.css'
import utils from '../util/request'
import regular from '../util/regular'
import common from '../util/common'
import helper from '../util/helper'
import mui from '@/assets/js/mui'
import FastClick from 'fastclick'
import vuex from 'vuex'
import store from '../store/'
Vue.prototype.mui = mui
import '../util/reset'
import Vant from 'vant';
import 'vant/lib/index.css';
import VueCookies from 'vue-cookies'
Vue.use(VueCookies)
//全局注册分页组件
Vue.prototype.request = utils
Vue.prototype.regular = regular
Vue.prototype.common = common
Vue.prototype.helper = helper
FastClick.attach(document.body)
Vue.use(Vant);
Vue.use(VueRouter)
Vue.use(vuex)
const history = window.localStorage;
let historyCount = history.getItem('count') * 1;
router.beforeEach(function (to, from, next) {
  // let token = VueCookies.get('token')
  let token = localStorage.getItem('token')
  if (to.path == "/" && token) {
    next({ path: '/index' });
  } else if (to.path != "/" && !token) {
    next({ path: '/' });
  } else {
    next();
  }
  const toIndex = history.getItem(to.path);
  const fromIndex = history.getItem(from.path);
  if (toIndex) {
    if (!fromIndex || parseInt(toIndex, 10) > parseInt(fromIndex, 10) || (toIndex === '0' && fromIndex === '0')) {
      store.commit('UPDATE_DIRECTION', { direction: 'forward' })
    } else {
      store.commit('UPDATE_DIRECTION', { direction: 'reverse' })
    }
  } else {
    ++historyCount;
    history.setItem('count', historyCount);
    to.path !== '/' && history.setItem(to.path, historyCount);
    store.commit('UPDATE_DIRECTION', { direction: 'forward' })
  }
  if (/\/http/.test(to.path)) {
    let url = to.path.split('http')[1];
    window.location.href = `http${url}`
  } else {
    next()
  }
});
new Vue({
  el: '#mobile',
  store,
  router,
  render: v => v(index),
})
